package com.core.config.crud;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.core.system.utils.ConvertUtils;
import com.pojo.parent.BaseDto;
import com.pojo.parent.BaseEntity;
import com.pojo.parent.PageResp;
import com.pojo.resp.RemoveResp;
import com.utility.exception.ServiceException;
import lombok.SneakyThrows;

import java.util.*;


/**
 * 核心增删改查服务接口实现
 * @author Huawei
 * @date 2023-12-31 22:22
 */
public abstract class CrudServiceImpl<M extends BaseMapper<T>, T extends BaseEntity, D extends BaseDto> extends ServiceImpl<M,T> implements CrudService<T,D>{


    protected Class<D> currentDtoClass() {
        return (Class<D>) ReflectionKit.getSuperClassGenericType(this.getClass(), 2);
    }


    protected Class<T> currentEtClass() {
        return (Class<T>) ReflectionKit.getSuperClassGenericType(this.getClass(), 1);
    }


    /**
     * 组装公共参数方法
     * @param paramMap 各自的实体类字段
     * @return
     */
    public abstract LambdaQueryWrapper<T> getLambdaQueryWrapper(Map<String,Object> paramMap);


    /**
     *  //TODO 校验逻辑 验证新增/编辑接口
     * @param dto 各自的实体类字段
     * @return
     */
    public abstract void verify(D dto);


    @SneakyThrows
    @Override
    public PageResp<D> page(Map<String,Object> paramMap){
        if (!paramMap.containsKey("pageNum") && !paramMap.containsKey("pageSize")){
            throw new ServiceException("参数异常");
        }
        if (Objects.isNull(paramMap.get("pageNum")) || Objects.isNull(paramMap.get("pageSize"))){
            throw new ServiceException("参数异常");
        }
        PageResp<D> pageResp = new PageResp<>();
        Page<T> page = page(new Page<>(Long.valueOf( paramMap.get("pageNum").toString()), Long.valueOf( paramMap.get("pageSize").toString())), getLambdaQueryWrapper(paramMap));
        if (Objects.isNull(page) ||Objects.isNull(page.getRecords())|| page.getRecords().isEmpty()){
            pageResp.setTotal(0);
            pageResp.setRecords(new ArrayList<>());
            return pageResp;
        }

        pageResp.setTotal((int)page.getTotal());
        List<?> records = page.getRecords();
        List<D> converts = ConvertUtils.Convert(records, currentDtoClass());
        pageResp.setRecords(converts);
        return  pageResp;
    }

    @Override
    public List<D> list(Map<String,Object> paramMap){
        List<T> list = super.list(this.getLambdaQueryWrapper(paramMap));
        return ConvertUtils.Convert(list, currentDtoClass());
    }


    public void updateById(D dto){
        verify(dto);
        super.updateById(ConvertUtils.Convert(dto, currentEtClass()));
    }

    public void insert(D dto){
        verify(dto);
        super.save(ConvertUtils.Convert(dto, currentEtClass()));
    }

    public void saveOrUpdate(D dto){
        verify(dto);
        super.saveOrUpdate(ConvertUtils.Convert(dto, currentEtClass()));
    }


    public void remove(Long id){
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        wrapper.select("id");
        wrapper.eq("id",id);
        List<T> list = super.list(wrapper);
        if (list.isEmpty()){
            throw  new ServiceException("数据不存在");
        }
        super.removeById(id);
    }

    public RemoveResp removeIds(Long[] id){
        if (id == null || id.length == 0){
            throw  new ServiceException("请选择删除项");
        }
        QueryWrapper<T> wrapper = new QueryWrapper<>();

        wrapper.in("id",Arrays.asList(id));
        wrapper.select("id");
        List<T> list = super.list(wrapper);
        super.removeByIds(Arrays.asList(id));
        return new RemoveResp("成功删除"+list.size()+"条数据。",-1);
    }

}
